﻿using System.Collections.Generic;
using System.Linq;
using BattleCity.Classes;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace BattleCity.GUI
{
    public class MenuScreen : Screen
    {
        Vector2 selectorOffset = new Vector2(40, 4);
        int speed = 1;

        //Элементы меню будем хранить в массиве элементов типа Menu
        List<MenuItem> _menu;
        //Текстуры и прямоугольники для вывода элементов меню
        Texture2D _selector, _menuBack;
        Vector2 _menuBackPos, _selectorPos;
        Rectangle _menuBackR;

        int _currentItem;
        public int CurrentItem
        {
            get { return _currentItem; }
            set
            {
                if (value < 1)
                    _currentItem = _menu.Count;
                else if (value > _menu.Count)
                    _currentItem = 1;
                else
                    _currentItem = value;
                SelectItem();
            }
        }

        public MenuScreen(BattleCityGame game, Texture2D selector, Texture2D menuBack, List<MenuItem> menuItems)
            :base(game)
        {
            _sprBatch = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));
            _currentItem = 1;

            _menu = menuItems;
            _selector = selector;
            _selectorPos = new Vector2(_menu[0].ImageVector.X - selectorOffset.X, _menu[0].ImageVector.Y - selectorOffset.Y);
            _menuBack = menuBack;
            _menuBackR = new Rectangle(0, 0, game.GameScreenSize.X, game.GameScreenSize.Y);
            _menuBackPos = new Vector2(0f, game.GameScreenSize.Y + 1);         
        }
        public void DoItemAction()
        {
            _menu[_currentItem - 1].Action();
            while (_menuBackPos.Y != 0)
                SlideScreen();
        }
        public override void Update(GameTime gameTime)
        {
            if (_menuBackPos.Y != 0)
                SlideScreen();
        }
        //Рисование элементов
        public override void Draw(GameTime gameTime)
        {
            //Вывод элементов меню
            foreach(MenuItem m in _menu)
                _sprBatch.Draw(m.ImageTexture, m.ImageVector, null, m.Color, 0f, new Vector2(0f, 0f), 1f, SpriteEffects.None, 0f);
            //Селектор меню
            _sprBatch.Draw(_selector, _selectorPos, null, Color.White, 0f, new Vector2(0f, 0f), 1f, SpriteEffects.None, 0f);
            //Вывод фона игры
            _sprBatch.Draw(_menuBack, _menuBackPos, null, Color.White, 0f, new Vector2(0f, 0f), 1f, SpriteEffects.None, 0.1f);
            base.Draw(gameTime);
        }
        private void SelectItem()
        {
            _menu.Where((m, i) => i != _currentItem - 1).ToList().ForEach(m => m.Color = Color.DarkGray);
            _menu[_currentItem - 1].Color = Color.White;
            _selectorPos = new Vector2(_menu[_currentItem - 1].ImageVector.X - selectorOffset.X, _menu[_currentItem - 1].ImageVector.Y - selectorOffset.Y);
        }
        private void SlideScreen()
        {
            _menuBackPos.Y -= speed;
            _selectorPos.Y -= speed;
            foreach (MenuItem m in _menu)
                m.ImageVector = new Vector2(m.ImageVector.X, m.ImageVector.Y - speed);
        }
    }
}